package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IAssetDetailFragmentModel;
import com.wgts.wgts.mvp.presenter.IAssetDetailFragmentPresenter;
import com.wgts.wgts.mvp.view.IAssetDetailFragmentView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.ShopKeepAssetReq;
import com.wgts.wgts.netmodle.resp.ShopKeepAssetEntity;
import com.wgts.wgts.netmodle.resp.WgtsKeepAssetEntity;

import java.util.List;

public class AssetDetailFragmentPresenter implements IAssetDetailFragmentPresenter {

    private IAssetDetailFragmentModel model;

    private IAssetDetailFragmentView view;

    private int pageIndex = 1;

    private int pageSize = 20;

    private ShopKeepAssetEntity assetEntity;

    private String flowType;


    public AssetDetailFragmentPresenter(IAssetDetailFragmentModel model, IAssetDetailFragmentView view) {
        this.model = model;
        this.view = view;
    }

    @Override
    public void initAssets(String flowType) {
        this.flowType = flowType;
        pageIndex = 1;
        ShopKeepAssetReq shopKeepAssetReq = new ShopKeepAssetReq();
        shopKeepAssetReq.flowType = flowType;
        shopKeepAssetReq.pageIndex = String.valueOf(pageIndex);
        shopKeepAssetReq.pageSize = String .valueOf(pageSize);

        model.initAsset(shopKeepAssetReq, new HttpResultSubscriber<List<WgtsKeepAssetEntity>>() {
            @Override
            public void onSuccess(List<WgtsKeepAssetEntity> entity) {
                if(entity.size() < pageSize){
                    view.assetResult(entity, true);
                }else{
                    view.assetResult(entity, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }

    @Override
    public void loadMore() {
        ShopKeepAssetReq shopKeepAssetReq = new ShopKeepAssetReq();
        shopKeepAssetReq.flowType = flowType;
        shopKeepAssetReq.pageIndex = String.valueOf(pageIndex);
        shopKeepAssetReq.pageSize = String .valueOf(pageSize);
        model.initAsset(shopKeepAssetReq, new HttpResultSubscriber<List<WgtsKeepAssetEntity>>() {
            @Override
            public void onSuccess(List<WgtsKeepAssetEntity> entity) {
                if(entity.size() < pageSize){
                    view.moreAssetResult(entity, true);
                }else{
                    view.moreAssetResult(entity, false);
                    pageIndex++;
                }
            }

            @Override
            public void _onError(int status, String msg) {
                view.onError(status, msg);
            }
        });
    }
}
